package com.blog.service.impl;

import com.blog.async.AsyncTask;
import com.blog.mapper.DiscussMapper;
import com.blog.model.DiscussInfo;
import com.blog.model.EmailModel;
import com.blog.pojo.Discuss;
import com.blog.service.DiscussService;
import com.blog.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author JiaFuJun
 * @version v1.0
 * @date 2020-06-08
 */
@Service
@Lazy
public class DiscussServiceImpl implements DiscussService {
    @Autowired
    private DiscussMapper discussMapper;
    @Autowired
    private AsyncTask asyncTask;

    @Override
    public void insert(Discuss record) {
        discussMapper.insert(record);
    }

    @Override
    public void update(Discuss record) {
        discussMapper.update(record);
    }

    @Override
    public void del(Object id) {
        discussMapper.del(id);
    }

    @Override
    public List<DiscussInfo> getByArticleId(Integer id) {
        return discussMapper.getByArticleId(id);
    }


    @Override
    public List<Discuss> getList(Discuss record) {
        return discussMapper.selectByRecord(record);
    }

    @Override
    public PageInfo<Discuss> pageList(Discuss record, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        List<Discuss> list = discussMapper.selectByRecord(record);
        return new PageInfo<>(list);
    }

    @Override
    public EmailModel getById(Integer id) {
        return discussMapper.getById(id);
    }

    @Override
    public void delByArtId(Object id) {
        discussMapper.delByArtId(id);
    }

    @Override
    public void handle(Discuss record) {
        if (record.getId() == null) {
            record.setDiscussTime(DateUtil.getStrTime());
            insert(record);
            //是否为回复邮件
            if (record.getReplyId() != null && !record.getReplyId().equals("")) {
                //异步发送回复邮件
                asyncTask.sendEmail(record);
            }
        } else {
            update(record);
        }
    }

}